package com.javacore;

/*
 Calculate Rectangle Area using Java Example
 This Calculate Rectangle Area using Java Example shows how to calculate
 area of Rectangle using it's length and width.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CalculateRectArea {
	static int width = 0;
	static int length = 0;
	static int area = 0;
	static boolean isWidth = false;
	static boolean isHeight = false;

	/**
	 * @param args
	 *            command line arguments
	 */
	public static void main(String[] args) {
		int in = inputWidthAndHeightOfRectangle(0);
		try {
			area = length * width;
			System.out.println("Area of a rectangle is " + area);
		} catch (Exception ex) {
			System.out.println("Error: " + ex.getMessage());
		}
	}

	public static int inputWidthAndHeightOfRectangle(int i) {
		try {
			if (isHeight == false) {
				// do {
				try {

					// read the length from console
					BufferedReader br = new BufferedReader(
							new InputStreamReader(System.in));
					System.out.println("Please enter length of a rectangle");
					length = Integer.parseInt(br.readLine());
					isHeight = true;

				} catch (Exception ex) {
					isHeight = false;
					System.out
							.println("Lenght value invalid!Please try again!");
				}
				// } while (!isHeight);
			}

			if (isHeight == false) {
				i = 0;
				return inputWidthAndHeightOfRectangle(i);
			}

			if (isHeight == true && isWidth == false) {
				// do {
				try {
					// read the width from console

					BufferedReader br = new BufferedReader(
							new InputStreamReader(System.in));
					System.out.println("Please enter width of a rectangle");
					width = Integer.parseInt(br.readLine());
					isWidth = true;

				} catch (Exception ex) {
					isWidth = false;
					System.out.println("Width value invalid!Please try again!");
				}
				// } while (!isWidth);
			}
			if (isHeight == true && isWidth == false) {
				i = 0;
				return inputWidthAndHeightOfRectangle(i);
			}
		}
		// if invalid value was entered
		catch (NumberFormatException ne) {
			System.out.println("Invalid value" + ne);
			System.exit(0);
		}
		return i;
	}
}

/*
 * Output of Calculate Rectangle Area using Java Example would be Please enter
 * length of a rectangle 10 Please enter width of a rectangle 15 Area of a
 * rectangle is 150
 */